// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import { g_bundleName } from "../mgr/assetsManager";
import GameMgr from "../mgr/gameManager";
import SoundMgr from "../mgr/soundManager";



const { ccclass, property } = cc._decorator;

//管理类基类 挂在主节点上 用于管理各种调度问题

@ccclass
export default class ComponentBase extends cc.Component {

    isOnly: boolean = false;


    onLoad() {
        this.setPropNode(this.node);

        // console.log(`name === ${this.name}_${this.node.uuid}`)
    }

    isOnlyOneNode() {
        return this.isOnly;
    }

    setPropNode(node: cc.Node) {

        // console.log('node.name.indexOf("_n_") = ',node.name.indexOf("_n_"));
        const name = node.name;
        const splitArr = name.split('_');

        const newName = splitArr.slice(1).join('_');
        // console.log('newName = ',newName);
        if (this[newName]) {
            console.error(`node :: ${newName} :: has been added in this node ${this.node.name}`);
        }
        if (splitArr.length > 1) {
            if (splitArr[0] === 'N') {
                this[newName] = node;
            } else if (splitArr[0] === 'L') {
                this[newName] = node.getComponent(cc.Label);
            } else if (splitArr[0] === 'S') {
                this[newName] = node.getComponent(cc.Sprite);
            } else if (splitArr[0] === 'BM') {
                this[newName] = node;
                this[newName].on('click', this._MenuBtnCallBack, this);
                const str = splitArr[1];
                if (this[`${str}CallBack`]) {
                    GameMgr.Ins().on(`${this.name}_${this.node.uuid}`, `${str}CallBack`, this[`${str}CallBack`].bind(this))

                } else {
                    console.error('no callback:', this.node.name, str);
                }

            } else if (splitArr[0] === 'BB') {
                this[newName] = node;
                this[newName].on('click', this._BackBtnCallBack, this);
                const str = splitArr[1];
                if (this[`${str}CallBack`]) {
                    GameMgr.Ins().on(`${this.name}_${this.node.uuid}`, `${str}CallBack`, this[`${str}CallBack`].bind(this))
                } else {
                    console.error('no callback:', this.node.name, str);
                }
            }
        }

        for (let i in node.children) {
            const child = node.children[i];
            this.setPropNode(child);
        }
    }

    _MenuBtnCallBack(target) {

        // console.log('base MenuBtnCallBack',target);
        SoundMgr.Ins().playEffect('btn_touch',g_bundleName.resources);
        this.sendBtnEvent(target)
    }

    _BackBtnCallBack(target) {

        // console.log('base BackBtnCallBack',target);

        this.sendBtnEvent(target)
    }

    sendBtnEvent(target) {
        const name = target.node.name;
        const splitArr = name.split('_');
        const event = `${splitArr[1]}CallBack`;
        splitArr.splice(0, 2);
        GameMgr.Ins().emitTarget(`${this.name}_${this.node.uuid}`, event, target, ...splitArr);
    }

    onDestroy(): void {
        GameMgr.Ins().offTarget(`${this.name}_${this.node.uuid}`);

        if (this.isOnlyOneNode()) {
            GameMgr.Ins().deleteOnelyOneNode(this.node);
        }
        SoundMgr.Ins().releaseAucioclip(this.node.name);

    }

}
